/*
 * @Author: zzd993
 * @Date: 2023-02-11 15:05:36
 * @LastEditors: zzd993
 * @LastEditTime: 2023-02-15 17:36:58
 * @FilePath: \手写vue2源码\src\observe\dep.js
 * Copyright (c) 2023 by ZZD, All Rights Reserved. 
 */

let id = 0

class Dep {
    constructor() {
        this.id = id++  // 属性的dep要收集watcher
        this.subs = []  // 这里存放的是当前属性对应的watcher有哪些
    }
    depend() {
        // 这里我们不希望放重复的watcher
        // this.subs.push(Dep.target)
        Dep.target.addDep(this)  // 让watcher记住dep
    }
    addSub(watcher) {
        this.subs.push(watcher)
    }
    notify() {
        this.subs.forEach(watcher => watcher.update())
    }
}
Dep.target = null

let stack = []
export function pushTarget(watch) {
    stack.push(watch)
    Dep.target = watch
}
export function popTarget() {
    stack.pop()
    Dep.target = stack[stack.length-1]
}
export default Dep